<?php

namespace App\Http\Controllers\Api\Common;

use App\Http\Controllers\Controller;
use App\Models\Banner;
use App\Models\Notice;
use App\Models\Setting;

class CommonController extends Controller
{
    /**
     * banner
     *
     * @return array
     */
    public function banner(): array
    {
        $where = [
            'status'=>1
        ];
        $data = Banner::query()->where($where)->oldest('sort')->get();
        $result = array();
        if($data){
            foreach ($data as $val){
                $result[] = Banner::getDataByID($val['id']);
            }
        }
        return ret($result);
    }

    /**
     * banner详情
     * @return array
     */
    public function bannerDetail(): array
    {
        $id = request('id',null);
        $where = [
            'id'=>$id,
        ];
        $model = Banner::query();
        ModelQuery($id,$model,$where);
        $data = Banner::getDataByID($id);
        return ret($data);
    }


    /**
     * Notice
     *
     * @return array
     */
    public function notice(): array
    {
        $where = [
            'status'=>1
        ];
        $data = Notice::query()->where($where)->oldest('sort')->get();
        $result = array();
        if($data){
            foreach ($data as $val){
                $result[] = Notice::getDataByID($val['id']);
            }
        }
        return ret($result);
    }

    /**
     * 公告详情
     *
     * @return array
     */
    public function noticeDetail(): array
    {
        $id = request('id',null);
        $where = [
            'id'=>$id,
        ];
        $model = Notice::query();
        ModelQuery($id,$model,$where);
        $data = Notice::getDataByID($id);
        return ret($data);
    }

    /**
     * 系统设置
     * @return array
     */
    public function systemSetting(): array
    {
        $key = request('key','');
        if(!$key){
            return retError('缺少必要参数');
        }
        $arr = [
            'customer_telephone','appointment_notice'
        ];
        if(!in_array($key,$arr)){
            return retError('无效的参数');
        }
        $where = [
            'status'=>1,
            'key'=>$key,
        ];
        $value = Setting::query()->where($where)->value('value');
        return ret($value);
    }
}
